{# form表单宏定义 #}

{# 分页html #}
{% macro pagination_html(pagination, is_row=True) -%}
    {% if is_row %}
    <div class="row">
        <div class="col-lg-12">
            {{ build_pagination_html(pagination) }}
        </div>
    </div>
    {% else %}
        {{ build_pagination_html(pagination) }}
    {% endif %}
{%- endmacro %}

{# 生成分页html #}
{% macro build_pagination_html(pagination) %}
<div class="pull-right">
{% if pagination.total > 0 %}
    <div class="btn-group" style="padding-right:10px;">
        <a href="javascript:;" class="btn btn-white">{{ pagination.total }}条</a>
        {% if pagination.has_prev %}<a href="{{ pagination_href(pagination.prev_num) }}" class="btn btn-white"><i class="fa fa-chevron-left"></i></a>{% endif %}
        {% for page in pagination.iter_pages() %}
            {% if page %}
                {% if page != pagination.page %}
                    <a href="{{ pagination_href(page) }}" class="btn btn-white">{{ page }}</a>
                {% else %}
                   <a href="javascript:;" class="btn btn-primary active">{{ page }}</a>
                {% endif %}
            {% else %}
                <a href="javascript:void(0);" class="btn btn-white">…</a>
            {% endif %}
        {% endfor %}
        {% if pagination.has_next %}<a href="{{ pagination_href(pagination.next_num) }}" class="btn btn-white"><i class="fa fa-chevron-right"></i></a>{% endif %}
    </div>

    <div class="btn-group" style="padding-left:12px; border-left: 1px solid #ccc;">
        {{ pagination_change_page_size(20) }}
        {{ pagination_change_page_size(50) }}
        {{ pagination_change_page_size(100) }}
    </div>
{% else %}
    <p>暂无数据</p>
{% endif %}
</div>
{% endmacro %}

{% macro pagination_href(page, page_size=None) -%}
{% set page_size = page_size if page_size else request.view_args.get('page_size', None) %}
{{ url_for(request.endpoint, page=page, page_size=page_size) }}{% if request.query_string %}{{ '?' + request.query_string }}{% endif %}
{%- endmacro %}

{% macro pagination_change_page_size(page_size) -%}
{% set current_page_size = request.view_args.get('page_size', None) %}
{% if current_page_size == page_size %}<a href="javascript:;" class="btn btn-primary active">{{ page_size }}</a>
{% else %}<a href="{{ pagination_href(1, page_size) }}" class="btn btn-white">{{ page_size }}</a>
{% endif %}
{%- endmacro  %}

{# 搜索表单 #}
{% macro build_search_form(form_config, action="", method="GET") %}
<form class="form" action="{{ action }}" method="{{ method }}" id="search_form">
    <div class="row">
        {% set args = request.args %}
        {% for field in form_config %}
            {% set type = field.get('type', 'input') %}
            {% set name = field.get('name', '') %}
            {% set value = args.get(name, '') %}
            {% set col_lg = field.get('col-lg', 'col-lg-2') %}

            {% if type == 'hidden' %}
                <input type="hidden" id="{{ name }}" name="{{ name }}" value="{{ value }}">
            {% else %}
            <div class="{{ col_lg }}">
                <div class="form-group">
                    <label>{{ field.get('label', ' ') }}</label>
                {% if type == 'input' %}
                    <input type="text" class="form-control" id="{{ name }}" name="{{ name }}" value="{{ value }}" >

                {% elif type == 'textarea' %}
                    <textarea class="form-control" id="{{ name }}" name="{{ name }}" >{{ value }}</textarea>

                {% elif type == 'select' %}
                    {% set options = field.get('options', []) %}
                    <select class="form-control" name="{{ name }}" id="{{ name }}" size="1">
                        {% for option in options %}
                            {% set option_val = '%s' % option.get('value', '') %}
                            {% set selected = 'selected' if value == option_val else '' %}
                            <option value="{{ option_val }}" {{ selected }}>{{ option.get('name') }}</option>
                        {% endfor %}
                    </select>
                {% elif type == 'daterange' %}
                    {% set begin_name = 'begin_'+name %}
                    {% set end_name = 'end_'+name %}
                    <div class="input-daterange input-group">
                        <input type="text" value="{{ args.get(begin_name, '') }}" name="{{ begin_name }}" class="form-control">
                        <span class="input-group-addon">至</span>
                        <input type="text" value="{{ args.get(end_name, '') }}" name="{{ end_name }}" class="form-control">
                    </div>
                {% elif type == 'compare' %}
                    {% set op_select = 'select_' + name %}
                    {% set op_select_value = args.get(op_select, '') %}
                    {% set op_select_options = [
                                    {'name':'请选择……', 'value':''},
                                    {'name':'小于 <', 'value':'lt'},
                                    {'name':'小于等于 <=', 'value':'lte'},
                                    {'name':'等于 ==', 'value':'eq'},
                                    {'name':'大于等于 >=', 'value':'gte'},
                                    {'name':'大于 >', 'value':'gt'},
                                    {'name':'不等于 !=', 'value':'neq'},
                                    ] %}
                    {% set op_text = 'text_' + name %}
                    {% set op_text_value = args.get(op_text, '') %}
                    <div class="input-group">
                        <select class="form-control" size="1" id="{{ op_select }}" name="{{ op_select }}">
                        {% for option in op_select_options %}
                            {% set option_val = '%s' % option.get('value', '') %}
                            {% set selected = 'selected' if op_select_value == option_val else '' %}
                            <option value="{{ option_val }}" {{ selected }}>{{ option.get('name') }}</option>
                        {% endfor %}
                        </select>
                        <span class="input-group-addon">-</span>
                        <input class="form-control" type="text" name="{{ op_text }}" value="{{ op_text_value }}">
                    </div>
                {% endif %}
                </div>
            </div>
            {% endif %}
        {% endfor %}
    </div>
    <div class="row"><div class="col-lg-12"><div class="hr-line-dashed"></div></div></div>
    <div class="row">
        <div class="col-lg-2">
            <div class="form-group">
                <button type="submit" class="btn btn-primary">提交</button>
            </div>
        </div>
    </div>
</form>
{% endmacro %}


{% macro input(label, name, value='', placeholder='', type='', required='', help='') %}
<div class="form-group">
    <label class="col-lg-4 control-label" id="label-{{ name }}">{{ label }}</label>
    <div class="col-lg-8" id="control-{{ name }}">
        <input type="text" class="form-control" id="{{ name }}"
            name="{{ name }}" value="{{ value }}"
            placeholder="{{ placeholder }}" type="{{ type }}" {{ required }}>
        {% if help %}<span class="help-block m-b-none">{{ help }}</span>{% endif %}
    </div>
</div>
{% endmacro %}

{% macro datepicker(label, name, value='', placeholder='', type='', required='', help='', class='input-daterange') %}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8">
        <div class="input-group {{class}}">
            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
            <input type="text" class="form-control" value="{{ value }}" id="{{ name }}" name="{{ name }}" placeholder="{{ placeholder }}" {{ required }}>
            {% if help %}<span class="help-block m-b-none">{{ help }}</span>{% endif %}
        </div>
    </div>
</div>
{% endmacro %}



{% macro select(label, name, value='', option_list=[], size='1', required='', help='', disabled='') %}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8">
        <select class="form-control" id="{{ name }}" name="{{ name }}" size="{{ size }}" {{ required }} {{ disabled }}>
        {% for option in option_list %}
            {% if option[1].__str__() == value.__str__() %}
                <option value="{{ option[1] }}" selected="selected">{{ option[0] }}</option>
            {% else %}
                <option value="{{ option[1] }}">{{ option[0] }}</option>
            {% endif %}
        {% endfor %}
        </select>
    </div>
</div>
{% endmacro %}


{% macro textarea(label, name, value='', required='', help='') %}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8">
        <textarea id="{{ name }}" class="form-control" name="{{ name }}" {{ required }}>{{ value }}</textarea>
        {% if help %}<span class="help-block m-b-none">{{ help }}</span>{% endif %}
    </div>
</div>
{% endmacro %}


{% macro form_label(label, value) %}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8">
        <label class="form-control">{{ value }}</label>
    </div>
</div>
{% endmacro %}


{% macro submit(btn='提交', errormsg='', id="submit") -%}
<div class="form-group">
    <div class="col-lg-4"></div>
    <div class="col-lg-8">
        <button type="submit" class="btn btn-primary" id="{{ id }}">{{ btn }}</button>
    </div>
</div>
{%- endmacro %}

